package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

type person struct {
	id int `uri:"id" binding:"required"`
	name string `uri:"name" binding:"required"`
}

type Person struct {
	ID   int    `uri:"id" binding:"required"`
	Name string `uri:"name" binding:"required"`
}

func main() {
	//router := gin.Default()
	//goodsGroup := router.Group("/goods")
	//goodsGroup.GET("/:name/:id", func(c *gin.Context) {
	//	var person Person
	//	if err := c.ShouldBindUri(&person); err != nil {
	//		c.Status(404)
	//		return
	//	}
	//	c.JSON(http.StatusOK, gin.H{
	//		"name": person.Name,
	//		"id":   person.ID,
	//	})
	//})
	//router.Run(":8082")
	router := gin.Default()
	goodsGroup := router.Group("/goods")
	{
		//goodsGroup.GET("/list", goodsList)
		goodsGroup.GET("/:id/:name", goodsList)
		goodsGroup.POST("/add", addGoods)
	}
	router.Run(":8082")
}

func goodsList(c *gin.Context)  {
	var p person
	if err := c.ShouldBindUri(&p); err != nil {
		fmt.Println(err)
		c.Status(404)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"id": p.id,
		"name": p.name,
	})
}

func goodsDetai(c *gin.Context)  {
	id := c.Param("id")
	action := c.Param("action")
	c.JSON(http.StatusOK, gin.H{
		"id": id,
		"action": action,
	})
}

func goodsDetai1(c *gin.Context)  {
	// get
	id := c.DefaultQuery("id", "hah")
	// post
	name := c.PostForm("name")

	c.JSON(http.StatusOK, gin.H{
		"id": id,
		"name": name,
	})

}

func addGoods(c *gin.Context)  {

}